package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishNew;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/14 10:39
 * @Desc: 菜品管理
 */
@RestController
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 查询菜品列表
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/dish/page")
    public R<Page<DishNew>> listAll(Integer page, Integer pageSize, String name) {
        Page<DishNew> dishPage = dishService.listAll(page, pageSize, name);

//        log.info("======================查询菜品列表========================");
//        System.out.println(dishPage.getRecords());

        return dishPage != null ? R.success(dishPage) : R.error("未查询到任何信息");
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/dish")
    public R<String> deleteDish(Long[] ids) {
        boolean flag = setmealDishService.findSetmeatlDishByDishId(ids);
        if (!flag)
            return R.error("请先删除关联的套餐");

        dishFlavorService.deleteDishFlavorByDishId(ids);
        int i = dishService.deleteDish(ids);

        return i != 0 ? R.success("删除成功") : R.error("删除失败");
    }

    /**
     * 修改菜品售卖状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/dish/status/{status}")
    public R<String> updateDishStatus(@PathVariable Integer status, Long[] ids) {
        int i = dishService.updateDishStatus(status, ids);

        return i != 0 ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 添加菜品
     *
     * @param dish
     * @return
     */
    @PostMapping("/dish")
    public R<String> addDish(@RequestBody DishNew dish) {
        int i = dishService.addDish(dish);

        return i != 0 ? R.success("添加成功") : R.error("添加失败");
    }

    /**
     * 根据id查询菜品信息
     *
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<DishNew> findDishById(@PathVariable Long id) {
//        log.info("=========================================");
//        log.info("id= ={}", id);

        DishNew dishNew = dishService.findDishById(id);

        return dishNew != null ? R.success(dishNew) : R.error("系统错误，请稍后再试");
    }

    /**
     * 修改菜品信息
     *
     * @param dishNew
     * @return
     */
    @PutMapping("/dish")
    public R<String> updateDishInfo(@RequestBody DishNew dishNew) {
        int i = dishService.updateDishInfo(dishNew);

        return i != 0 ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 根据菜品类别id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/dish/list")
    public R<List<Dish>> findDishByCaregoryId(Long categoryId, String name, Integer status) {
        List<Dish> dishes = dishService.findDishByCaregoryId(categoryId, name, status);

        return dishes != null ? R.success(dishes) : R.error("请求失败，请稍后再试");
    }
}
